//
//  ZYNViewControllerForPlayBase.m
//  VideoPic
//
//  Created by dllo on 16/3/25.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ZYNViewControllerForPlayBase.h"
#import "ZYNTableViewCellForBase.h"
#import "NetworkingHandle.h"
#import "ZYNModelForSongList.h"
#import <YYWebImage.h>
#import "zynAVPlayerSingleton.h"
#import "ZYNViewControllerForTapDetail.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface ZYNViewControllerForPlayBase ()<UITableViewDataSource, UITableViewDelegate, ZYNViewControllerForTapDetailDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *imageForHeader;
@property (nonatomic, strong) UIImageView *imageForIcon;
@property (nonatomic, strong) UILabel *labelForOwner;
@property (nonatomic, strong) UILabel *labelForTitle;
@property (nonatomic, strong) UILabel *labelForListencount;
@property (nonatomic, strong) UILabel *labelForDesc;
@property (nonatomic, strong) UIImageView *imageForPic;
@property (nonatomic, strong) UIButton *buttonForBack;
@property (nonatomic, strong) NSMutableArray *arrForSongs;
@property (nonatomic, strong) NSString *SongUrl;
@property (nonatomic, strong) UIView *viewForPlayInfo;
@property (nonatomic, strong) UIImageView *imageForSongPic;
@property (nonatomic, strong) UILabel *labelForSongName;
@property (nonatomic, strong) UILabel *labelForSingerName;
@property (nonatomic, strong) UIButton *buttonForPlayOrPause;
@property (nonatomic, strong) UIButton *buttonForList;
@property (nonatomic, strong) UIImageView *imageViewForBack;
@property (nonatomic, strong) UIVisualEffectView *effectView;
@property (nonatomic, strong) ZYNModelForSongList *model;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) NSInteger random;

@end

@implementation ZYNViewControllerForPlayBase

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = YES;
    self.arrForSongs = [NSMutableArray array];
    self.random = 0;
    
    self.SongUrl = [NSString stringWithFormat:@"http://api.songlist.ttpod.com/songlists/%@?address=辽宁省大连市沙河口区五一路314号&agent=none&longitude=121.54475&net=2&from=android&os=5.0.1&v=v8.4.0.2016022715&alf=700145&api_version=1.0&imei=358851054311604&latitude=38.883408&f=508&resolution=1080x1920&language=zh&user_id=0", self.strForSongId];
    if (self.selected == NO) {
    
        [self handleData];
        
    } else {
        
        [self handleListenData];
        
    }
    [self createTableView];
    [self createHeaderView];
    [self createBottomView];
//    [self addProgressObserver];
}
- (void)handleListenData {
    self.arrForSongs = [NSMutableArray array];
    NSString *str = [NSString stringWithFormat:@"http://api.favorite.ttpod.com/favorite/song/plaza?address=辽宁省大连市沙河口区五一路314号&agent=none&longitude=121.54474&utdid=UwlxIo9OkikBAIGWDJh6Ypjh&net=2&from=android&os=5.0.1&v=v8.4.0.2016022715&alf=700145&api_version=1.0&imei=358851054311604&latitude=38.883392&f=508&resolution=1080x1920&language=zh&random=%ld&user_id=0", self.random];
    
    [NetworkingHandle handleWorkData:str AndBody:nil value:nil handleBlock:^(id result) {
        self.imageForHeader.yy_imageURL = [NSURL URLWithString:[[[result objectForKey:@"data"] objectForKey:@"image"] objectForKey:@"pic"]];
        
        NSMutableArray *arrForTemp = [[result objectForKey:@"data"] objectForKey:@"songs"];
        for (NSDictionary *dic in arrForTemp) {
            ZYNModelForSongList *model = [[ZYNModelForSongList alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrForSongs addObject:model];
        }
        [self.tableView reloadData];
    }];
    
   
}
- (void)handleData {
    
    [NetworkingHandle handleWorkData:self.SongUrl AndBody:nil value:nil handleBlock:^(id result) {
        
        self.imageForHeader.yy_imageURL = [NSURL URLWithString:[[result objectForKey:@"image"] objectForKey:@"pic"]];

        self.imageForIcon.yy_imageURL = [NSURL URLWithString:[[result objectForKey:@"owner"] objectForKey:@"portrait_pic"]];
        self.labelForOwner.text = [[result objectForKey:@"owner"] objectForKey:@"nick_name"];
        self.labelForTitle.text = [result objectForKey:@"title"];
        if ([[result objectForKey:@"desc"] isEqual:[NSNull null]]) {
            self.labelForDesc.text = @"";
        }else {
            self.labelForDesc.text = [NSString stringWithFormat:@"%@", [result objectForKey:@"desc"]];
        }
        CGFloat count = [[result objectForKey:@"listen_count"] floatValue];
  
        self.labelForListencount.text = [NSString stringWithFormat:@"%.1f万", count / 10000];
      
        for (NSDictionary *dicc in [result objectForKey:@"songs"]) {
            ZYNModelForSongList *model = [[ZYNModelForSongList alloc] init];
            NSArray *arr = [dicc objectForKey:@"urlList"];
            if (arr.count == 0) {
                NSLog(@"123456");
            
            } else {
            
                [model setValuesForKeysWithDictionary:dicc];
            
            
                [self.arrForSongs addObject:model];
            }
        }
        [self.tableView reloadData];
        NSLog(@"123:%@", self.arrForSongs);
        
    }];
    
}
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[ZYNTableViewCellForBase class] forCellReuseIdentifier:@"pool1"];

}
- (void)createHeaderView {
    // 背景图片
    self.imageForHeader = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH / 3 * 2)];
    self.imageForHeader.userInteractionEnabled = YES;
    self.imageForHeader.backgroundColor = [UIColor lightGrayColor];
    self.tableView.tableHeaderView = self.imageForHeader;
//    self.imageForHeader.image = [UIImage imageNamed:@""]
    // 返回按钮
    self.buttonForBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.imageForHeader addSubview:self.buttonForBack];
    self.buttonForBack.frame = CGRectMake(WIDTH / 18, WIDTH  / 14, WIDTH / 12, WIDTH / 12);
    [self.buttonForBack setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [self.buttonForBack addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
    if (self.selected == NO) {
        
        // 歌手头像
        self.imageForIcon = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH / 18, WIDTH / 6, WIDTH / 10, WIDTH / 10)];
        [self.imageForHeader addSubview:self.imageForIcon];
        self.imageForIcon.layer.masksToBounds = YES;
        self.imageForIcon.layer.cornerRadius = WIDTH / 10 / 2;
        
        // 歌手工作室
        self.labelForOwner = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 18 + WIDTH / 10 + 10 , WIDTH / 6, WIDTH / 3, WIDTH / 12)];
        [self.imageForHeader addSubview:self.labelForOwner];
        self.labelForOwner.font = [UIFont systemFontOfSize:13];
        self.labelForOwner.textColor = [UIColor whiteColor];
        // 标题
        self.labelForTitle = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 18, WIDTH / 6 +  WIDTH / 10 + 10, WIDTH - 100, WIDTH / 12)];
        [self.imageForHeader addSubview:self.labelForTitle];
        self.labelForTitle.textColor = [UIColor whiteColor];
        // 听图标
        self.imageForPic = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH / 18, WIDTH / 6 +  WIDTH / 10 + 10 + WIDTH / 12 + 5, WIDTH / 16, WIDTH / 16)];
        self.imageForPic.image = [UIImage imageNamed:@"Ting"];
        [self.imageForHeader addSubview:self.imageForPic];

        // 收听个数
        self.labelForListencount = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 18 + WIDTH / 14 + 5, WIDTH / 6 +  WIDTH / 10 + 10 + WIDTH / 12 + 5,  WIDTH / 8,  WIDTH / 14)];
        [self.imageForHeader addSubview:self.labelForListencount];
        self.labelForListencount.textColor = [UIColor whiteColor];
        self.labelForListencount.font = [UIFont systemFontOfSize:13];
        
        // 歌单简介
        self.labelForDesc = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 18 + WIDTH / 14 + 5 + WIDTH / 10 + 10, WIDTH / 6 +  WIDTH / 10 + 10 + WIDTH / 12 + 5, WIDTH / 2 + 50, WIDTH / 14)];
        [self.imageForHeader addSubview:_labelForDesc];
        self.labelForDesc.textColor = [UIColor whiteColor];
        self.labelForDesc.font = [UIFont systemFontOfSize:13];
        
        
    } else {
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, self.imageForHeader.frame.size.height / 4 * 3, WIDTH, self.imageForHeader.frame.size.height / 4)];
        view.backgroundColor = [UIColor colorWithRed:29 / 256.f green:29 / 256.f  blue: 28 / 256.f alpha:1];
        [self.imageForHeader addSubview:view];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        button.frame = CGRectMake(view.frame.size.width / 5, 10, view.frame.size.width / 8, view.frame.size.height / 2 - 5);
//        button.backgroundColor = [UIColor orangeColor];
        [view addSubview:button];
        [button setImage:[UIImage imageNamed:@"huan"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(handleChangClick:) forControlEvents:UIControlEventTouchUpInside];
        
        
        UILabel *labelForChange = [[UILabel alloc] initWithFrame:CGRectMake(view.frame.size.width / 5, view.frame.size.height / 2 + 5, view.frame.size.width / 8, view.frame.size.height / 2 - 10)];
        labelForChange.text = @"换一批";
        labelForChange.font = [UIFont systemFontOfSize:14];
        labelForChange.textColor = [UIColor whiteColor];
        [view addSubview:labelForChange];
        
    }
    
}
// 换一批button的点击事件
- (void)handleChangClick:(UIButton *)button {
    
    NSLog(@"dddddd");
    
    self.random = 1;
    [self handleListenData];
    
}
- (void)createBottomView {
    
    // 底边的view
    self.viewForPlayInfo = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - 60, WIDTH, 60)];
    [self.view addSubview:self.viewForPlayInfo];
    self.viewForPlayInfo.userInteractionEnabled = YES;
    
    // 底边view上的子控件
    self.imageViewForBack = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    self.imageViewForBack.userInteractionEnabled = YES;
    [self.viewForPlayInfo addSubview:self.imageViewForBack];
    
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    self.effectView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    [self.imageViewForBack addSubview:self.effectView];
    self.effectView.userInteractionEnabled = YES;
    
    // 添加手势
    UITapGestureRecognizer *tap  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [self.effectView addGestureRecognizer:tap];
    
    self.imageForSongPic = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, WIDTH  / 8 - 10, WIDTH / 8 - 10)];
    [self.effectView addSubview:self.imageForSongPic];
    
    // 歌曲图片
    self.imageForSongPic.backgroundColor = [UIColor lightGrayColor];
    self.imageForSongPic.layer.masksToBounds = YES;
    self.imageForSongPic.layer.cornerRadius = (WIDTH  / 8 - 10) / 2;
    
    // 歌名
    self.labelForSongName = [[UILabel alloc] initWithFrame:CGRectMake(15 + 10 + WIDTH  / 8 - 10 , 5, WIDTH / 2, WIDTH / 15)];
    [self.effectView addSubview:self.labelForSongName];
    self.labelForSongName.font = [UIFont systemFontOfSize:15];
    
    // 歌手名
    self.labelForSingerName = [[UILabel alloc] initWithFrame:CGRectMake(15 + 10 + WIDTH / 8 - 10, WIDTH / 15 + 5, WIDTH / 4, WIDTH / 14)];
    
    [self.effectView addSubview:self.labelForSingerName];
    self.labelForSingerName.font = [UIFont systemFontOfSize:14];
    
    // 播放暂停按钮
    self.buttonForPlayOrPause = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonForPlayOrPause.frame = CGRectMake(WIDTH - 100, 20, WIDTH / 16, WIDTH / 16);
    [self.buttonForPlayOrPause setImage:[UIImage imageNamed:@"bofang"] forState:UIControlStateNormal];
    [self.effectView addSubview:self.buttonForPlayOrPause];
    
    [self.buttonForPlayOrPause addTarget:self action:@selector(handlePlayOrPause:) forControlEvents:UIControlEventTouchUpInside];
    self.buttonForPlayOrPause.selected = YES;
    
    // 收藏
    self.buttonForList = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonForList.frame = CGRectMake(WIDTH - 60, 20, WIDTH / 16, WIDTH / 16);
    [self.buttonForList setImage:[UIImage imageNamed:@"dislike"] forState:UIControlStateNormal];
    [self.effectView addSubview:self.buttonForList];
    [self.buttonForList addTarget:self action:@selector(handleCollection:) forControlEvents:UIControlEventTouchUpInside];
    ZYNModelForSongList *model = [self.arrForSongs firstObject];
    NSLog(@"%@", model);
    self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
    self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];

    self.labelForSongName.text = model.name;
    self.labelForSingerName.text = model.singerName;
    
    
}
- (void)handleCollection:(UIButton *)button {
    
    
    
    
}
// 最上面的返回按钮的点击事件
- (void)handleBack:(UIButton *)button {
    [[zynAVPlayerSingleton sharePlayer].player pause];
    [self.navigationController popViewControllerAnimated:YES];
    
}
// 底边的view的手势响应事件
- (void)handleTap:(UIGestureRecognizer *)tap {
    
    ZYNViewControllerForTapDetail *detail = [[ZYNViewControllerForTapDetail alloc] init];
    detail.model = self.model;
    detail.arr = self.arrForSongs;
    detail.xiaBiao = self.index;
    detail.delegate = self;
    [self presentViewController:detail animated:YES completion:nil];

}

// 底边view的播放暂停按钮点击事件
- (void)handlePlayOrPause:(UIButton *)button {
    
    if (button.selected == YES) {
        button.selected = !button.selected;
        [button setImage:[UIImage imageNamed:@"bofang"] forState:UIControlStateNormal];
        [[zynAVPlayerSingleton sharePlayer].player pause];
    } else {
        button.selected = !button.selected;
        [button setImage:[UIImage imageNamed:@"zanting"] forState:UIControlStateNormal];
        [[zynAVPlayerSingleton sharePlayer].player play];
        
    }
    
}

// 代理方法
- (void)handleButtonClick:(ZYNModelForSongList *)model {
   
    self.imageViewForBack.yy_imageURL = [NSURL URLWithString:model.picUrl];
    self.imageForSongPic.yy_imageURL = [NSURL URLWithString:model.picUrl];

    self.labelForSongName.text = model.name;
    self.labelForSingerName.text = model.singerName;
    
}

#pragma mark - UITableView delegate methods
#pragma mark ** two request dataSoure delegate methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrForSongs.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYNTableViewCellForBase *cell  = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    ZYNModelForSongList *model = [self.arrForSongs objectAtIndex:indexPath.row];
    [self.buttonForPlayOrPause setImage:[UIImage imageNamed:@"zanting"] forState:UIControlStateNormal];
    self.buttonForPlayOrPause.selected = YES;
    cell.labelOfCount.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    [cell passModel:model];
    
    return cell;
}
#pragma mark ** other system delegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//height for cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return WIDTH / 5;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   
    self.model = [self.arrForSongs objectAtIndex:indexPath.row];
    if (self.model.urlList.count == 0) {
        NSLog(@"123456");
        [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[self.model.mvList objectAtIndex:1] objectForKey:@"url"]];
    } else {
        [[zynAVPlayerSingleton sharePlayer] playerWithUrl:[[self.model.urlList objectAtIndex:1] objectForKey:@"url"]];
        NSLog(@"abcdefg");
    }
    NSString *str = [NSString stringWithFormat:@"%ld", indexPath.row];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changLabelForColor" object:nil userInfo:@{@"index":str}];
    
    [self.buttonForPlayOrPause setImage:[UIImage imageNamed:@"zanting"] forState:UIControlStateNormal];
    self.buttonForPlayOrPause.selected = YES;

    self.imageViewForBack.yy_imageURL = [NSURL URLWithString:self.model.picUrl];
    self.imageForSongPic.yy_imageURL = [NSURL URLWithString:self.model.picUrl];
    self.index = indexPath.row;
    self.labelForSongName.text = self.model.name;
    self.labelForSingerName.text = self.model.singerName;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"showPause" object:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
